package com.wsh.generator.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsh.base.DnycDto;
import com.wsh.entity.generator.GenFileConfEntity;
import com.wsh.generator.GenFileConfService;
import com.wsh.mapper.generator.GenFileConfDao;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
* @author 王思涵
* @date 2020-02-22
*/
@Service
public class GenFileConfServiceImpl extends ServiceImpl<GenFileConfDao, GenFileConfEntity> implements GenFileConfService {


    @Override
    public void saveConf(DnycDto<GenFileConfEntity> dnycDto, String tableName) {

        List<GenFileConfEntity> fileConfig = dnycDto.getTablesData();
        String[] removeIds = dnycDto.getRemoveIds();

        // 附件删除项
        if (removeIds != null && removeIds.length > 0) {
            removeByIds(Arrays.asList(removeIds));
        }

        // 数据保存
        if (fileConfig != null && fileConfig.size() > 0) {
            fileConfig.stream().forEach(e -> e.setTableName(tableName));
            saveOrUpdateBatch(fileConfig);
        }
    }

    @Override
    public List<GenFileConfEntity> getByTableName(String tableName) {
        Map<String, Object> maps = new HashMap<>();
        maps.put("table_name", tableName);
        return baseMapper.selectByMap(maps);
    }
}
